package com.vito.oa.email.util;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.search.StringTerm;

public class OaSubjectTerm extends StringTerm{

	private static final long serialVersionUID = -4492606254645924868L;

	protected OaSubjectTerm(String pattern) {
		super(pattern);
	}
	
	private boolean emailMatch(String s, String pattern, boolean ignoreCase) {
		int len = s.length() - pattern.length();
		for (int i=0; i <= len; i++) {
		    if (s.regionMatches(ignoreCase, i, 
					pattern, 0, pattern.length()))
			return true;
		}
		return false;
	}

	@Override
	public boolean match(Message msg) {
		try {
			if (emailMatch(msg.getSubject(), pattern, true)) {
				return true;
			}
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
		return false;
	}

}
